<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索按钮区域 -->
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="orderList" border stripe>
        <!-- 编号 -->
        <el-table-column type="index" label="#"></el-table-column>
        <!-- 订单编号 -->
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <!-- 订单价格 -->
        <el-table-column width="120px" label="订单价格" prop="order_price"></el-table-column>
        <!-- 支付状态 -->
        <el-table-column width="100px" label="是否付款" prop="pay_status">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.pay_status===0">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <!-- 发货状态 -->
        <el-table-column width="90px" label="是否发货" prop="is_send"></el-table-column>
        <!-- 下单时间 -->
        <el-table-column width="150px" label="下单时间" prop="update_time">
          <template slot-scope="scope">{{scope.row.update_time*1000|dateFormat}}</template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column width="140px" label="操作" prop>
          <template slot-scope>
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="editOrderList"></el-button>
            <el-button type="success" size="mini" icon="el-icon-location" @click="showProgressBox"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 编辑对话框 -->
    <el-dialog title="修改地址" :visible.sync="editDialogVisible" width="50%" @close="dialogClosed">
      <!-- 对话框表单区域 -->
      <el-form
        :model="editOrderForm"
        :rules="editOrderFormRules"
        ref="editOrderFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="editCity">
          <!-- 选择省市的级联选择器 -->
          <el-cascader v-model="editOrderForm.editCity" :options="cityData"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="editOrderForm.address"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 显示发货进度对话框 -->
    <el-dialog title="查看物流进度" :visible.sync="progressDialogVisible" width="50%">
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.ftime"
        >{{activity.context}}</el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>
<script>
import cityData from '../order/citydata.js'
export default {
  // 数据
  data() {
    return {
      // 订单列表
      orderList: [],
      //   查询信息
      queryInfo: {
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 10
      },
      //   数据总数
      total: 0,
      //  编辑对话框
      editDialogVisible: false,
      //   对话框表单
      editOrderForm: {
        editCity: [],
        address: ''
      },
      //  表单验证规则
      editOrderFormRules: {
        editCity: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请填写详细的物流地址', trigger: 'blur' }
        ]
      },
      //   省市区数据
      cityData,
      //   发货进度对话框
      progressDialogVisible: false,
      //   物流进度信息
      progressInfo: [],
      reverse: true
    }
  },
  //   方法
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      this.orderList = res.data.goods
      this.total = res.data.total
      console.log(this.orderList)
    },
    // 改变每页条数
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 改变当前页面
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    //  编辑事件
    editOrderList() {
      this.editDialogVisible = true
    },
    // 修改地址对话框关闭
    dialogClosed() {
      this.$refs.editOrderFormRef.resetFields()
    },
    // 物流进度对话框
    async showProgressBox() {
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) {
        this.$message.error('获取物流信息失败！')
      }
      this.progressInfo = res.data
      this.progressDialogVisible = true
      console.log(this.progressInfo)
    }
  },
  //   生命周期函数
  created() {
    this.getOrderList()
  }
}
</script>
<style lang="less" scoped>
.el-pagination {
  margin: 20px;
}
.el-cascader {
  width: 100%;
}
</style>
